home *** CD-ROM | disk | FTP | other *** search
- Path: s02.pavilion.co.uk!usenet
- From: AJRobb@pavilion.co.uk (Andy J Robb)
- Newsgroups: comp.lang.c
- Subject: Re: To malloc (new) or not to malloc? Clarification. Please ignore previous post.
- Date: Wed, 10 Jan 1996 21:44:05 GMT
- Organization: Pavilion Internet plc
- Message-ID: <4d1bsf$28s@s02.pavilion.co.uk>
- References: <4ctvk3$ort@maverick.tad.eds.com> <4cua5t$dm@maverick.tad.eds.com>
- NNTP-Posting-Host: poolb35.pavilion.co.uk
- X-Newsreader: Forte Free Agent 1.0.82
-
- fignet05.darrins@eds.com (Darrin Smith) wrote:
-
- >Why is it that you can do something like the following:
-
- > char *x;
-
- > x="Some really long string with no particular meaning";
-
- x now POINTS to this string it does not contain any data itself.
-
- >and have no problems, but can't (safely) do something like this:
-
- > struct st1{char one[10];
- > char two[20];
- > char three[10];
- > };
-
- > st1 *sptr; //or struct st1 *sptr in C instead of C++
-
- You now have an unitialized pointer - reusing old space on the stack
- for itself. It does not point at any data space. Chances are it will
- be pointing somewhere into your program! By the way, this is C++, in
- C you need:
-
- struct st1 *sptr;
-
- > fread(sptr,sizeof(st1),1,infile);
-
- If sptr is pointing to somewhere outside your data space, a good
- operating system (not DOS) will immediately terminate the program.
-
- >This caused a program I was trying to debug to crash. When I allocated
- >memory for sptr (I used sptr=new st1; but I suppose malloc would have done
- >just as well) it worked fine.
-
-
- >What is going on here? Why isn't memory set aside for st1 *sptr just as
- >it is for char *x?
-
- No memory was set asside for *x, it was made to point to a string. In
- your case, the string was effectively constant - you should have
- declared x as:
-
- const char *x = "your long string . . . ";
-
- This will try to prevent you program from overwriting data that may be
- in the text (code) area. This is a common method of reducing memory
- requirement and speeding program startup - all concurrent copies of
- the program can use the same memory for such strings.
-
- >Before I did the new (or malloc) it seemed as though
- >my program was getting written over!
-
- new is a C++ operator - it does not work in ANSI or K&R C.
-
- malloc() requests memory to be assigned (like new).
-
- struct st1 *sptr = malloc(sizeof(struct st1));
-
- fread(sptr, sizeof(struct st1), 1, infile);
-
- This will sort of work - however there are no guarantees - best not do
- it. Many compilers leave differing gaps within a struct between
- members. Thus:
-
- (sizeof(struct st1) == 40) may not be true.
-
- Remember, the memory allocated by malloc stays allocated until free()
- (or realloc()) is called.
-
- Regards,
- -----BEGIN PGP PUBLIC KEY BLOCK-----
- Version: 2.6.2i
-
- mQCNAy/MpRwAAAEEAOt6uBYqT8yv9EmqNhK8m6v+bYi8QjnGW3Bo6iU1gsMj5pa6
- MHgq99c8deADbE3cbJ6uZS9v5pZE3WCf6HCQjlB5iULA5RZzMdAumd/WUzuL9UT3
- B44D9EqqFIL79FlYb56v4oKFqFp1/J2bIpYUwnUvabGzGjdLrpPl4P16x9sNAAUR
- tCNBbmR5IEogUm9iYiA8QUpSb2JiQHBhdmlsaW9uLmNvLnVrPrQhQW5keSBSb2Ji
- IDxBSlJvYmJAcGF2aWxpb24uY28udWs+
- =/wVD
- -----END PGP PUBLIC KEY BLOCK-----
-
-